package com.songtech.auth.resource.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.songtech.auth.resource.model.Resources;
import com.songtech.auth.resource.service.ResourceService;
import com.songtech.auth.util.CommState;
import com.songtech.base.common.BaseConstant;
import com.songtech.base.common.JsonResult;

@Controller
@RequestMapping("/auth/resource")
public class AuthResourceController {
	
	@Autowired
	private ResourceService resourceService;
	
	/**
	 * 新增资源
	 * @param resource
	 * @return
	 */
	@RequestMapping("/add")
	@ResponseBody
	public Object addResources(Resources resource){
		return new JsonResult(resourceService.addResource(resource), BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS);
	}
	
	/**
	 * 修改资源
	 * @param resource
	 * @return
	 */
	@RequestMapping("/update")
	@ResponseBody
	public Object updateResources(Resources resource){
		return new JsonResult(resourceService.update(resource), BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS);
	}
	
	/**
	 * 查询所有资源
	 * @return
	 */
	@RequestMapping("/getAllResource")
	@ResponseBody
	public Object getAllResource(){
		List<Resources> resList = resourceService.queryAllResources();
		JsonResult result = new JsonResult(resList);
		result.status = BaseConstant.SUCCESS_CODE;
		result.msg = BaseConstant.SUCCESS;
		return result;
	}
	
	/**
	 * 根据资源ID查询资源详情
	 * @param resId
	 * @return
	 */
	@RequestMapping("/getResourceById")
	@ResponseBody
	public Object getResourceById(String resId){
		return new JsonResult(resourceService.getResourceById(resId), BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS);
	}
	
	/**
	 * 启用资源
	 * @param resId
	 * @return
	 */
	@RequestMapping("/open")
	@ResponseBody
	public Object enableResource(String resId){
		return new JsonResult(resourceService.enabledResource(resId), BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS);
	}
	
	/**
	 * 停用资源，需要将此资源的所有子集资源停用
	 * @param resId
	 * @return
	 */
	@RequestMapping("/stop")
	@ResponseBody
	public Object disableResource(String resId){
		return new JsonResult(resourceService.disabledResource(resId), BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS);
	}
	
	/**
	 * 删除资源，需要将此资源的所有子集资源删除(逻辑删除)
	 * @param resId
	 * @return
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public Object deleteResource(String resId){
		return new JsonResult(resourceService.deleteResource(resId), BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS);
	}
	
	/**
	 * 获取资源类型
	 * @return
	 */
	@RequestMapping("/resourceType")
	@ResponseBody
	public JsonResult getResourceType(){
		return new JsonResult(CommState.getResourceType(), BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS);
	}
	
	/**
	 * 获取资源状态
	 * @return
	 */
	@RequestMapping("/resourceState")
	@ResponseBody
	public JsonResult getResourceState(){
		return new JsonResult(CommState.getResourceState(), BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS);
	}
}
